package com.monkey.web.context;


import com.monkey.utils.PropertiesLoader;
import com.monkey.web.ApplicationConstants;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 全局配置类
 */
public class Global {

    /**
     * 保存全局属性值
     */
    private static ConcurrentMap<String, String> map = new ConcurrentHashMap<>();

    /**
     * 属性文件加载对象
     */
    private static PropertiesLoader propertiesLoader = new PropertiesLoader(ApplicationConstants.MONKEY_CONFIG);

    private static Boolean resourceDomain = null;

    private static String resourceHost = null;
    private static String resourceTemp = null;
    private static String resourcePrePath = null;
    private static String projectPath = null;


    /**
     * 获取配置
     */
    public static String getConfig(String key) {
        String value = map.get(key);
        if (value == null) {
            value = propertiesLoader.getProperty(key);
            map.put(key, value);
        }
        return value;
    }

    public static boolean getResourceDomain() {
        if (resourceDomain == null) {
            resourceDomain = propertiesLoader.getBoolean("resource.domain", false);
        }
        return resourceDomain;
    }

    public static String getResourceHost() {
        if (resourceHost == null) {
            resourceHost = propertiesLoader.getProperty("resource.host");
        }
        return resourceHost;
    }


    public static String getResourceTemp() {
        if (resourceTemp == null) {
            resourceTemp = propertiesLoader.getProperty("resource.temp");
        }
        return resourceTemp;
    }

    public static String getResourcePrePath() {
        if (resourcePrePath == null) {
            resourcePrePath = propertiesLoader.getProperty("resource.prePath");
        }
        return resourcePrePath;
    }


    public static String getProjectPath() {
        if (projectPath == null) {
            projectPath = propertiesLoader.getProperty("project.path");
        }
        return projectPath;
    }


}
